package com.demo;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import net.sf.jasperreports.export.ExporterInput;
import net.sf.jasperreports.export.OutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;

public class Demo {

	public static void main(String[] args) throws JRException {
		System.out.println(System.getProperty("user.dir"));
		InputStream is = Demo.class.getResourceAsStream("/MyReport.jrxml");
		JasperReport jasperReport = JasperCompileManager.compileReport(is);
		Map<String, Object> params = new HashMap<String, Object>();
		List<Object> list = new ArrayList<Object>();
		JRDataSource dataSource = new JRBeanCollectionDataSource(list, true);
		// print文件
		JasperPrint print = JasperFillManager.fillReport(jasperReport, params, dataSource);
		{
			JRXlsxExporter exporter = new JRXlsxExporter();
			ExporterInput exporterInput = new SimpleExporterInput(print);
			exporter.setExporterInput(exporterInput);
			OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput("d:\\asfdsf1.xlsx");
			exporter.setExporterOutput(exporterOutput);
			exporter.exportReport();
		}
		{
			JRDocxExporter exporter = new JRDocxExporter();
			ExporterInput exporterInput = new SimpleExporterInput(print);
			exporter.setExporterInput(exporterInput);
			OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput("d:\\asfdsf1.docx");
			exporter.setExporterOutput(exporterOutput);
			exporter.exportReport();

		}

	}
}
